﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EShop.Net.Util.Date
{
    /// <summary>
    /// 时间工具类
    /// </summary>
    public static class DateTimeHelper
    {
        /// <summary>
        /// 北京时间转换成unix时间戳(10位/秒)
        /// </summary>
        /// <param name="dateTime">时间</param>
        /// <returns>10位long类型时间戳</returns>
        public static long BeijingTimeToUnixTimeStamp10(DateTime dateTime)
        {
            return (long)(dateTime - new DateTime(1970, 1, 1, 8, 0, 0)).TotalSeconds;
        }

        /// <summary>
        /// 格林威治时间转换成unix时间戳(10位/秒)
        /// </summary>
        /// <param name="dateTime">时间</param>
        /// <returns>10位long类型时间戳</returns>
        public static long UtcTimeToUnixTimeStamp10(DateTime dateTime)
        {
            return (long)(dateTime - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
        }

        /// <summary>
        /// 北京时间转换成unix时间戳(13位/毫秒)
        /// </summary>
        /// <param name="dateTime">时间</param>
        /// <returns>13位long类型时间戳</returns>
        public static long BeijingTimeToUnixTimeStamp13(DateTime dateTime)
        {
            return (long)(dateTime - new DateTime(1970, 1, 1, 8, 0, 0)).TotalMilliseconds;
        }

        /// <summary>
        /// 格林威治时间转换成unix时间戳(13位/毫秒)
        /// </summary>
        /// <param name="dateTime">时间</param>
        /// <returns>13位long类型时间戳</returns>
        public static long UtcTimeToUnixTimeStamp13(DateTime dateTime)
        {
            return (long)(dateTime - new DateTime(1970, 1, 1, 0, 0, 0)).TotalMilliseconds;
        }

        /// <summary>
        /// 10位unix时间戳转换成北京时间
        /// </summary>
        /// <param name="unixTimeStamp">10位时间戳</param>
        /// <returns></returns>
        public static DateTime UnixTimeStamp10ToBeijingTime(long unixTimeStamp)
        {
            return new DateTime(1970, 1, 1, 8, 0, 0).AddSeconds(unixTimeStamp);
        }

        /// <summary>
        /// 10位unix时间戳转换成格林威治
        /// </summary>
        /// <param name="unixTimeStamp">10位时间戳</param>
        /// <returns></returns>
        public static DateTime UnixTimeStamp10ToUtcTime(long unixTimeStamp)
        {
            return new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(unixTimeStamp);
        }

        /// <summary>
        /// 13位unix时间戳转换成北京时间
        /// </summary>
        /// <param name="unixTimeStamp">13位时间戳</param>
        /// <returns></returns>
        public static DateTime UnixTimeStamp13ToBeijingTime(long unixTimeStamp)
        {
            return new DateTime(1970, 1, 1, 8, 0, 0).AddMilliseconds(unixTimeStamp);
        }

        /// <summary>
        /// 13位unix时间戳转换成格林威治
        /// </summary>
        /// <param name="unixTimeStamp">13位时间戳</param>
        /// <returns></returns>
        public static DateTime UnixTimeStamp13ToUtcTime(long unixTimeStamp)
        {
            return new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds(unixTimeStamp);
        }

        /// <summary>
        /// 判断是否比指定时间晚
        /// </summary>
        /// <param name="date"></param>
        /// <param name="other"></param>
        /// <returns></returns>
        public static bool IsAfter(this DateTime date, DateTime other)
        {
            return date > other;
        }
    }
}
